import { storage } from '@/utils/storage'
import routes from '@/routes'
import api from '@/api/apiList'
import { parseQueryToString, matchRouteByTree } from '@/utils'
export const filterTabs = (state, action) => {
    const history = action.history || { location: {} }
    const newState = JSON.parse(JSON.stringify(state))
    const { panes } = newState;
    const type = action.value
    let { activeKey } = newState;
    let newPanes = panes
    if (type === '1') { // 关闭当前页签
        if (activeKey === '/') return {
            panes: newPanes,
            activeKey: activeKey,
            history,
            openKeys: type === '3' ? [] : state.openKeys
        }
        newPanes = panes.filter(el => el.key !== activeKey || el.key === '/')
        activeKey = newPanes[newPanes.length - 1].key
        history.push(newPanes[newPanes.length - 1].key)
    }
    if (type === '2') { // 关闭其他页签
        newPanes = panes.filter(el => el.key === activeKey || el.key === '/')
        // history.push(activeKey)
    }
    if (type === '3') { // 关闭所有页签
        newPanes = [{ title: '首页', key: '/', closable: false }]
        if (activeKey !== '/') {
            activeKey = '/'
        }
    }
    storage.set('tabList', newPanes)
    return {
        panes: newPanes,
        activeKey,
        history,
        openKeys: type === '3' ? [] : state.openKeys
    }
}
export const addTabs = (state, action) => {
    const history = action.history
    const newState = JSON.parse(JSON.stringify(state))
    const { panes } = newState;
    let {
        activeKey,
        title,
        defaultSelectedKeys,
        keyPath,
        selectedKeys,
        query,
        defaultOpenKeys } = action
    let exist = panes.filter(el => el.key === activeKey)
    const { currentRoute } = matchRouteByTree(routes, activeKey)
    // console.log(action, 'existRoute');
    if (!exist.length) {
        panes.push({
            title: currentRoute.meta.title || '用户中心',
            key: activeKey, query: query || panes.query,
            meta: currentRoute.meta
        });
    }
    // console.log(activeKey, history, 'activeKey');
    if (activeKey && title) {
        storage.set('tabList', panes)
        let path = activeKey
        path += parseQueryToString(query)
        // 是否关闭打开页签
        !action.isCloseOpen && history.push(path)
    }
    return {
        panes,
        activeKey,
        keyPath,
        selectedKeys,
        defaultSelectedKeys,
        defaultOpenKeys,
        history: history || {}
    }
}
export const removeTabs = (state, action) => {
    const history = action.history
    const newState = JSON.parse(JSON.stringify(state))
    let { activeKey } = newState;
    const targetKey = action.activeKey
    let lastIndex;
    newState.panes.forEach((pane, i) => {
        if (pane.key === targetKey) {
            lastIndex = i - 1;
        }
    });
    const panes = newState.panes.filter(pane => pane.key !== targetKey);
    if (panes.length && activeKey === targetKey) {
        if (lastIndex >= 0) {
            activeKey = panes[lastIndex].key;
        } else {
            activeKey = panes[0].key;
        }
        // 是否关闭打开页签
        !action.isCloseOpen && history.push(activeKey)
    }
    // console.log(panes, activeKey, targetKey, 'panes');
    storage.set('tabList', panes)
    return {
        panes,
        activeKey,
        history
    }
}
export const changeTabs = (state, action) => {
    const newState = JSON.parse(JSON.stringify(state))
    let { activeKey } = action
    if (action.history) {
        const panes = newState.panes.filter(pane => pane.key === activeKey);
        let query = panes[0] && panes[0].query || {}
        let queryStr = parseQueryToString(query)
        // console.log(activeKey + queryStr, 'panes[0].query');
        action.history.push(activeKey + queryStr)
    }
    return {
        activeKey: activeKey,
        selectedKeys: action.selectedKeys || [activeKey],
        openKeys: action.openKeys || ['/' + action.activeKey.split('/')[1]],
    }
}

export const logout = async (state, action) => {
    await api.userCenter.user.logout()
    storage.remove('token', 'cookie')
    storage.remove('userInfo')
    action.history.push('/login')
    storage.set('tabList', routes.filter(el => el.affix).map(el => ({
        key: el.path,
        title: el.meta.title,
        closable: !el.affix,
        meta: el.meta
    })))
}

export const setUserInfo = (state, action) => {
    let userInfo = {
        ...state.userInfo,
        ...action.value
    }
    storage.set('userInfo', userInfo)
    return {
        ...state,
        userInfo
    }
}